﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.Users
{
    public class PresenterModifyUserView
    {
        IModifyBasicView viewModifyBasic;
        Model.User modifyUser = new Model.User() { Person = new Model.Person() };
        IModifyUserView view;

        public PresenterModifyUserView(IModifyUserView View, Model.User Kardex)
        {
            view = View;
            modifyUser = Kardex;
        }


        public void GoStepTwo()
        {
            view.ModifyBasic.ChargeDataToBasic();
            modifyUser = view.ModifyBasic.UserToModify;
            if (!PassFirstStep())
                view.CallFirstPage();
            else
                view.CallSecondPage();
            ProcedureAfterCall();
        }

        public void ModifyUser()
        {
            view.ModifyBasic.ChargeDataToBasic();
            modifyUser = view.ModifyBasic.UserToModify;
            if (!PassFirstStep())
            {
                view.CallFirstPage();
                ProcedureAfterCall();
            }
            else if (!PassSecondStep())
            {
                view.CallSecondPage();
                ProcedureAfterCall();
            }
            else
            {
                ManagerUser.UpdateUser(modifyUser);
                view.SystemMessage("El usuario fue actualizado exitosamente.");
                view.ReturnSearchUser();
            }
        }

        private bool PassFirstStep()
        {
            try
            {
                Model.User mp = new Model.User ();
                mp.Person = view.ModifyBasic.UserToModify.Person;
                ManagerUser.VerifyDataPerson(mp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(string.Format(ex.Message, "Actualizar"));
                return false;
            }
        }

        private bool PassSecondStep()
        {
            try
            {
                Model.User mp = new Model.User();
                string antiguoLogin = view.ModifyBasic.UserToModify.OldLogin;
                string nuevoLogin = view.ModifyBasic.UserToModify.Login;

                mp.Login = view.ModifyBasic.UserToModify.Login;
                mp.Password = view.ModifyBasic.UserToModify.Password;
                mp.RetryPassword = view.ModifyBasic.UserToModify.RetryPassword;
                mp.Role = view.ModifyBasic.UserToModify.Role;

                if ((antiguoLogin != nuevoLogin) && (antiguoLogin != null))
                    ManagerUser.VerifyDataUserModify(mp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(string.Format(ex.Message, "Actualizar"));
                return false;
            }
        }

        public void GoBackPage()
        {
            viewModifyBasic.GoBackPage();
        }

        public void GoNextPage()
        {
            viewModifyBasic.GoNextPage();
        }
        public void GoStepOne()
        {
            view.ModifyBasic.ChargeDataToBasic();
            modifyUser = view.ModifyBasic.UserToModify;
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        private void ProcedureAfterCall()
        {
            if (viewModifyBasic != null)
                modifyUser = viewModifyBasic.UserToModify;
            viewModifyBasic = view.ModifyBasic;
            viewModifyBasic.UserToModify = modifyUser;
            viewModifyBasic.Update();
        }

        public void FirstTime()
        {
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        public void NotifyErrorMessage(string MessageValue)
        {
            view.ErrorMessage(MessageValue);
        }
        public void NotifySystemMessage(string MessageValue)
        {
            view.SystemMessage(MessageValue);
        }
    }
}
